package com.mkvld.appietijdenv2;

import android.app.AlertDialog;
import android.view.View;
import android.widget.NumberPicker;

import com.mkvld.appietijdenv2.dialogs.DialNewShiftV9;

public class DialNewShiftV14 extends DialNewShiftV9 {
	NumberPicker startHour, startMin, endHour, endMin;
	
	
	@Override
	protected int getLayoudId() {
		return R.layout.dial_new_shift;
	}
	
	@Override
	protected void initView(View root) {
		startHour = (NumberPicker) root.findViewById(R.id.dns_from_hours);
		startHour.setMinValue(0);
		startHour.setMaxValue(23);
		startHour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(getEnd() > getStart());
			}
		});
		startHour.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		startHour.setValue((int)shift.getStart());
		
		startMin = (NumberPicker)root.findViewById(R.id.dns_from_min);
		startMin.setMinValue(0);
		startMin.setMaxValue(3);
		startMin.setDisplayedValues(new String[] {"00", "15", "30", "45"});
		startMin.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				if (oldVal == 3 && newVal == 0)
					setStartHour((getStartHour() + 1) % 24);
				if (oldVal == 0 && newVal == 3)
					setStartHour((getStartHour() + 23) % 24);
				dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(getEnd() > getStart());
				
			}
		});
		startMin.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		startMin.setWrapSelectorWheel(true);
		startMin.setValue((int) (shift.getStart() % 1 * 4));
		
		endHour = (NumberPicker) root.findViewById(R.id.dns_to_hours);
		endHour.setMinValue(0);
		endHour.setMaxValue(23);
		endHour.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(getEnd() > getStart());
			}
		});
		endHour.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		endHour.setValue((int)shift.getEnd());
		
		endMin = (NumberPicker)root.findViewById(R.id.dns_to_min);
		endMin.setMinValue(0);
		endMin.setMaxValue(3);
		endMin.setDisplayedValues(new String[] {"00", "15", "30", "45"});
		endMin.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				if (oldVal == 3 && newVal == 0)
					setEndHour((getEndHour() + 1) % 24);
				if (oldVal == 0 && newVal == 3)
					setEndHour((getEndHour() + 23) % 24);
				dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(getEnd() > getStart());
				
			}
		});
		endMin.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		endMin.setWrapSelectorWheel(true);
		endMin.setValue((int) (shift.getEnd() % 1 * 4));
	}
	@Override
	protected int getStartHour() {
		return startHour.getValue();
	}
	@Override
	protected void setStartHour(int startHour) {
		this.startHour.setValue(startHour);
	}
	@Override
	protected int getStartMin() {
		return startMin.getValue();
	}
	@Override
	protected void setStartMin(int startMin) {
		this.startMin.setValue(startMin);
	}
	@Override
	protected int getEndHour() {
		return endHour.getValue();
	}
	@Override
	protected void setEndHour(int endHour) {
		this.endHour.setValue(endHour);
	}
	@Override
	protected int getEndMin() {
		return endMin.getValue();
	}
	@Override
	protected void setEndMin(int endMin) {
		this.endMin.setValue(endMin);
	}
}
